package com.dal.producer.controller;

import com.dal.message.UserInfo;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
//import com.rocket.producer.message.UserInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@Slf4j
public class UserRegister {
    @Resource
    ObjectMapper mapper;
    @Resource
    private RocketMQTemplate rocketMQTemplate;
    @GetMapping("/newUser")
    public String userReg(@RequestParam() String name,@RequestParam() String nickname) throws JsonProcessingException {
//        模拟前端接收用户注册信息
//        var userInfo=UserInfo.builder().name(name).nickname(nickname).build();
        var userInfo=new UserInfo(name,nickname);
//        String user=mapper.writeValueAsString(userInfo);
        log.info(">> user "+userInfo);
//        发送消息到rocketmq，主题为users
//        rocketMQTemplate.convertAndSend("users", user );
        String replyString =rocketMQTemplate.sendAndReceive("users:userReg", userInfo ,String.class,30*1000);

        return replyString;
    }
}
